package com.oascarmovie;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;

import com.oascarmovie.model.CatalogModel;
import com.oascarmovie.widget.CatalogWidget;

public class CatalogSearchActivity extends Activity {
  
  private final static String[] CATALOG_BY_AWARD = {"最佳影片", "最佳导演",
    "最佳男主角", "最佳女主角", "最佳男配角", "最佳女配角", "最佳原创剧本", "最佳改编剧本"};
  private final static String[] CATALOG_BY_CATEGORY = {"爱情", "喜剧", "动作", "动画", 
    "灾难", "剧情", "恐怖", "科幻", "战争", "悬疑", "名著", "警匪", "家庭", "纪录历史", 
    "古装", "伦理", "传记", "奇幻", "冒险", "歌舞", "音乐", "儿童", "其他", "舞台艺术", 
    "惊栗", "情色", "犯罪惊悚", "黑色电影", "西部", "短片", "运动", "纪录片", "同性", "荒诞", "新闻" };
  private final static String[] CATALOG_BY_COUNTRY = {"美国", "英国", "法国", "德国", "中国", 
    "日本", "波兰", "瑞士", "原西德", "韩国", "俄罗斯", "津巴布韦", "南非", "冰岛", "巴哈马", 
    "塞尔维亚和黑山", "捷克斯洛伐克", "瑞典", "西德", "澳大利亚", "新西兰", "印度", "苏联", 
    "加拿大", "巴西", "印度尼西亚", "芬兰", "阿根廷", "意大利", "西班牙", "土耳其", "荷兰", "卢森堡", 
    "墨西哥", "比利时", "以色列", "丹麦", "挪威", "菲律宾", "卢森堡", "南斯拉夫", "奥地利"};
    
	private Button backButton;
	private CatalogWidget catalogByCalender;
  private CatalogWidget catalogByAward;
  private CatalogWidget catalogByCategory;
  private CatalogWidget catalogByCountry;
  
  private Handler buildCatalogHandler;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.catalogsearch);
        
        loadWidget();
        build();
        render(new Bundle());
    }
    
    public void switchToSearchView() {
//      Intent intent = new Intent();
//      intent.setClass(SearchResultActivity.this, SearchActivity.class);
//      startActivity(intent);
    	finish();
    }

	protected void loadWidget() {
		backButton = (Button)findViewById(R.id.button_search_by_catalog);		
	}

	protected void render(Bundle bundle) {
	  LoadingThread loadingThread = new LoadingThread(this.buildCatalogHandler);
	  loadingThread.start();
	}

  protected void build() {
    this.backButton.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        switchToSearchView();
      }
    });
    
    this.buildCatalogHandler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
        switch (msg.what) {
        case Constants.MESSAGE_WHAT_LOADING:
          prepareLoadingForCatalog();
          break;
        case Constants.MESSAGE_WHAT_REQUEST_CATALOG:
          @SuppressWarnings("unchecked")
          Map<String, List<CatalogModel>> allCatalogMap = (Map<String, List<CatalogModel>>) msg.obj;
          buidlCatalog(allCatalogMap);
          break;
        }

        super.handleMessage(msg);
      }
    };     
  }
  
  private void prepareLoadingForCatalog() {
    //this.itemDetailLoadingView.setVisibility(View.VISIBLE);

    // start thread
    // Here is to stop the previous thread
    /*if (loadItemDetailHtmlThread != null) {
      loadItemDetailHtmlThread.setStop(true);
    }*/

    LoadCatalogThread loadCatalogThread = new LoadCatalogThread(this.buildCatalogHandler);
    loadCatalogThread.start();
  }

  
  private void buidlCatalog(Map<String, List<CatalogModel>> allCatalogMap) {
    List<CatalogModel> calendarList = allCatalogMap.get(Constants.CATALOG_CALENDAR);
    List<CatalogModel> awardList = allCatalogMap.get(Constants.CATALOG_AWARD);
    List<CatalogModel> catetoryList = allCatalogMap.get(Constants.CATALOG_CATEGORY);
    List<CatalogModel> countryList = allCatalogMap.get(Constants.CATALOG_COUNTRY);
    this.catalogByCalender = new CatalogWidget(this, calendarList, this.getResources().getString(R.string.catalog_by_calendar), 25, true);
    this.catalogByAward = new CatalogWidget(this, awardList, this.getResources().getString(R.string.catalog_by_award), 15, true);
    this.catalogByCategory = new CatalogWidget(this, catetoryList, this.getResources().getString(R.string.catalog_by_category), 30, true);
    this.catalogByCountry = new CatalogWidget(this, countryList, this.getResources().getString(R.string.catalog_by_country), 25, true);
    
    ViewGroup mainView = ((LinearLayout)this.findViewById(R.id.table_catalog));
    //LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    mainView.addView(this.catalogByCalender);
    mainView.addView(this.catalogByAward);
    mainView.addView(this.catalogByCategory);
    mainView.addView(this.catalogByCountry);
  }
  
  // This is for testing
  private Map<String, List<CatalogModel>> buildCatalogMap() {
    Map<String, List<CatalogModel>> allCatalogMap = new HashMap<String, List<CatalogModel>>();

    List<CatalogModel> calendarList = new ArrayList<CatalogModel>();
    for (int i = 0; i < 89; i++) {
      CatalogModel catalogModel = new CatalogModel();
      catalogModel.setId(i);
      String text = String.format("%2d", i + 1);
      catalogModel.setName("第" + text + "届");
      calendarList.add(catalogModel);
    }
    allCatalogMap.put(Constants.CATALOG_CALENDAR, calendarList);

    List<CatalogModel> awardList = new ArrayList<CatalogModel>();
    for (int i = 0; i < CATALOG_BY_AWARD.length; i++) {
      CatalogModel catalogModel = new CatalogModel();
      catalogModel.setId(i);
      catalogModel.setName(CATALOG_BY_AWARD[i]);
      awardList.add(catalogModel);
    }
    allCatalogMap.put(Constants.CATALOG_AWARD, awardList);

    List<CatalogModel> catetoryList = new ArrayList<CatalogModel>();
    for (int i = 0; i < CATALOG_BY_CATEGORY.length; i++) {
      CatalogModel catalogModel = new CatalogModel();
      catalogModel.setId(i);
      catalogModel.setName(CATALOG_BY_CATEGORY[i]);
      catetoryList.add(catalogModel);
    }
    allCatalogMap.put(Constants.CATALOG_CATEGORY, catetoryList);

    List<CatalogModel> countryList = new ArrayList<CatalogModel>();
    for (int i = 0; i < CATALOG_BY_COUNTRY.length; i++) {
      CatalogModel catalogModel = new CatalogModel();
      catalogModel.setId(i);
      catalogModel.setName(CATALOG_BY_COUNTRY[i]);
      countryList.add(catalogModel);
    }
    allCatalogMap.put(Constants.CATALOG_COUNTRY, countryList);
    
    return allCatalogMap;
  }


}
